package com.dd.cloud.common.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * @author: HuYi.Zhang
 * @create: 2018-04-30 15:56
 **/
public class CodecUtils {

    public static String md5Hex(String data, String salt) {
        if (StringUtils.isBlank(salt)) {
            salt = data.hashCode() + "";
        }
        return DigestUtils.md5Hex(salt + DigestUtils.md5Hex(data));
    }

    public static String md5Hex(String data) {
        if (StringUtils.isBlank(data)) {
            return "";
        }
        return DigestUtils.md5Hex(data);
    }

    public static String shaHex(String data, String salt) {
        if (StringUtils.isBlank(salt)) {
            salt = data.hashCode() + "";
        }
        return DigestUtils.sha512Hex(salt + DigestUtils.sha512Hex(data));
    }

    public static String generateSalt() {
        return StringUtils.replace(UUID.randomUUID().toString(), "-", "");
    }

    /**
     * 验签
     *
     * @param signStr
     * @param sign
     * @return
     */
    public static boolean checkSign(String signStr, String sign) {
        String signStay = md5Hex(signStr);
        System.out.println(signStay);
        return signStay.equals(sign);
    }
}
